#### FOR FIGURE 3 IN THE MANUSCRIPT:

rm(list=ls())
library(foreign)
library(ggplot2) 
library(gridExtra)
library(knitr)
library(grid)
library(dplyr)
library(forcats)
#install.packages("extrafont")
library(extrafont)
#install.packages("performance")
library("performance")
library("see")
library("haven")

setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates")
setwd("~/Dropbox/EUI/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Estimates")

### First the theme:
theme_results <- theme_bw(base_size = 15) +
  theme(text=element_text(family="Times",
                          size = 15),
        axis.text.x = element_text(size = 15, color = "black"),
        axis.text.y = element_text(size = 15, color = "black",angle=0),
        axis.title.y = element_text(size = 18, color = "black"),
        axis.title.x = element_text(size = 18, color = "black"),
        legend.position = "top",
        legend.key.size = unit(1.2, "cm"),
        legend.text = element_text(size = 18),
        legend.title = element_text(size = 18),
        panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        strip.text.y.right = element_text(angle = 0)
  )

### We first plot the results of the TWFE regression estimates:
all2 <- read_dta("distance2.dta")

all2$id2 <- factor(all2$inc,
                   levels = c(0,1,2),
                   labels = c("Below\naverage","All", "Above\naverage"))

all2$y2 <- factor(all2$charter,
                  levels = c(0,1,2),
                  labels = c("Charter\nschools",
                             "Public\nschools",
                             "Overall\naverage"))

all2$charter2 <- all2$charter + 1
all2$charter2[all2$charter2>2] <- 0

all2$y2 <- factor(all2$charter2,
                  levels = c(0,1,2),
                  labels = c("Overall\naverage",
                             "Charter\nschools",
                             "Public\nschools"))

palette2 = c("black", "blue", "red")

plotall2 <- all2 %>%
  ggplot(aes(x = y2, y = coef, colour = id2)) +   
  geom_point(size = 2 ,position = position_dodge(.5)) +
  geom_errorbar(aes(ymax = ci_upper95, ymin = ci_lower95), width = 0,  size = .7, position = position_dodge(width = .5)) +
  geom_errorbar(aes(ymax = ci_upper90, ymin = ci_lower90), width = 0, size = 1.6, position = position_dodge(width = .5)) +
  scale_color_manual(name = "Neighborhoods\nby income:",values = palette2)+
  geom_hline(yintercept = 0, 
             linetype = 2, color = "black") + 
  xlab("") +
  ylab("Effect of increasing distance to closest\nbetting house on educational performance") +
  theme_results + 
  theme(strip.background =element_rect(fill="white"),
        axis.title.y = element_text(size = 25, color = "black"),
        axis.text.x = element_text(size = 25))
plotall2

### Now the descriptives:
setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Data")

toplot1 <- read_dta("gambling_data.dta")
toplot1$pau <- as.numeric(toplot1$pau)
toplot1$renta2 <- ordered(toplot1$renta2,
                          levels = c(0,1),
                          labels = c("Areas<average income", "Areas>average income"))
toplot1$publico <- ordered(toplot1$publico,
                           levels = c(1,2),
                           labels = c("Charter\nschools", "Public\nschools"))


theme_results <- theme_bw(base_size = 12) +
  theme(text=element_text(family="Times",size = 12),
        axis.text.x = element_text(size = 12, color = "black"),
        axis.text.y = element_text(size = 12, color = "black"),
        axis.title.y = element_text(size = 21, color = "black"),
        axis.title.x = element_text(size = 21, color = "black"),
        legend.position = "bottom",
        legend.key = element_rect(fill = NA),
        legend.text = element_text(size = 21),
        legend.title = element_text(size = 21),
        strip.text.y.right = element_text(angle = 0),
        strip.background = element_rect(fill = "white"),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        legend.background=element_blank(),
        strip.text.x = element_text(size=21))

descr1 <- toplot1%>%
  ggplot(aes(x = logdist_year, y = pau))+
  geom_smooth(method="lm", se=T, color="blue")+
  geom_jitter(alpha=0.1)+
  theme_minimal()+
  xlab("Logged distance to the closest betting house")+
  ylim(c(4,8)) +
  theme(legend.position='bottom')+
  ylab("Educational performance:\nschools' avg. Grade (0-10)") +
  theme_results + 
  theme(panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank())

palette2 = c("red", "black")

descr2 <- toplot1%>%
  ggplot(aes(x = logdist_year, y = pau, color=publico))+
  geom_smooth(method="lm", se=T)+
  geom_jitter(alpha=0.1)+
  scale_color_manual(name = "",values = palette2) +
  theme_minimal()+
  xlab("Logged distance to the closest betting house")+
  ylim(c(4,8)) +
  guides(color=guide_legend(override.aes=list(fill=NA)))+
  ylab("Educational performance:\nschools' avg. Grade (0-10)") +
  facet_wrap(.~renta2) +
  theme_results +
  theme(strip.text.x = element_text(size=18))

g <- arrangeGrob(descr1, descr2, nrow=2) #generates g
figure3 <- arrangeGrob(g, plotall2, ncol=2)
figure3
#setwd("~/Dropbox/Side projects/BET HOUSES PROJECT/data/Gambling_Replication_Materials/Figures")
#ggsave(file="whatever2_120321.png", plot=g2, width = 40, height = 25, units = "cm") #saves g
